package com.knowif.backend.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.knowif.backend.service.BannerService;
import com.knowif.pojo.Banner;
import com.knowif.pojo.BannerExample;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;

/**
 * @author ：Xh
 * @date ：Created in 2019/5/6 0006 20:07
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @PostMapping("/query")
    public RestBean<?> query(@RequestBody SearchCondition searchCondition){
        try {
            BannerExample bannerExample = ExampleUtil.buildExample(new BannerExample(), searchCondition);
            PageInfo<Banner> pageInfo = new PageInfo<>(bannerService.selectBanner(bannerExample));
            return new RestBean<>(RestBean.success_code, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new RestBean<List<Banner>>(RestBean.bad_code, "参数错误");
    }

    @PostMapping("/save")
    public RestBean<?> save(@RequestBody Banner banner){
        if(bannerService.saveBanner(banner)){
            return new RestBean<Object>(RestBean.success_code);
        }
        return new RestBean<Object>(RestBean.bad_code, "添加失败");
    }

    @PostMapping("/update")
    public RestBean<?> update(@RequestBody Banner banner){
        if(bannerService.updateBannerNotNull(banner)){
            return new RestBean<Object>(RestBean.success_code);
        }
        return new RestBean<Object>(RestBean.bad_code, "更新失败");
    }

    @DeleteMapping("/{id}")
    public RestBean<?> delete(@PathVariable("id") int id){
        if (bannerService.deleteBanner(id)){
            return new RestBean<Object>(RestBean.success_code);
        }
        return new RestBean<Object>(RestBean.bad_code, "删除失败");
    }

}
